package visiopuce.ui;

import java.util.ArrayList;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

import visiopuce.objets.Personne;
import visiopuce.ui.view.DossierEditor;

public class MemePersonneDialog extends Dialog {

	private Table table;
	ArrayList<Personne> personnes;

	public MemePersonneDialog(Shell parent, ArrayList<Personne> personnes) {
		super(parent);
		this.personnes = personnes;
		setShellStyle(SWT.CLOSE | SWT.TITLE | SWT.BORDER | SWT.APPLICATION_MODAL);
		setBlockOnOpen(true);
	}

	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText("Personnes du même nom");
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		table = new Table(container, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		table.addMouseListener(new MouseListener() {

			public void mouseUp(MouseEvent e) {
			}

			public void mouseDown(MouseEvent e) {
			}

			public void mouseDoubleClick(MouseEvent e) {
				try {
					TableItem[] selections = table.getSelection();
					for (int j = 0; j < selections.length; j++) {
						TableItem ti = selections[j];
						int idPersonne = Integer.parseInt(ti.getText());
						IEditorInput editorInput = new DossierEditorInput(idPersonne);
						PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().openEditor(editorInput, DossierEditor.ID, true, IWorkbenchPage.MATCH_INPUT);
						IEditorPart editeurActif = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
						if (editeurActif instanceof DossierEditor) {
							DossierEditor editeur = (DossierEditor) editeurActif;
							if (editeur.getDossierTabItem() != null) {
								editeur.getTabFolder().setSelection(editeur.getDossierTabItem().getTabItem());
							}
						}
					}
				} catch (PartInitException e1) {
					e1.printStackTrace();
				}
			}
		});
		GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
		data.heightHint = 200;
		table.setLayoutData(data);
		String[] titles = { "id", "Nom", "Prénom", "Date de naissance", "IPP" };
		for (int i = 0; i < titles.length; i++) {
			TableColumn column = new TableColumn(table, SWT.NONE);
			column.setText(titles[i]);
		}
		if (personnes != null) {
			for (Personne personne : personnes) {
				TableItem item = new TableItem(table, SWT.NONE);
				item.setText(0, "" + personne.getIdPersonne());
				item.setText(1, personne.getNom());
				item.setText(2, personne.getPrenom());
				item.setText(3, "" + personne.getDateNaissance());
				item.setText(4, "" + personne.getiPP());
			}
			for (int j = 0; j < titles.length; j++) {
				table.getColumn(j).pack();
			}
		}
		return container;
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		Button bOk = createButton(parent, IDialogConstants.OK_ID, "Nouvelle personne", true);	
		bOk.setToolTipText("La personne est bien nouvelle et ne fait pas partie de cette liste");
		Button bCl = createButton(parent, IDialogConstants.CANCEL_ID, "Annuler", false);
		bCl .setToolTipText("Annuler l'enregistrement");
	}

	@Override
	protected Point getInitialSize() {
		return new Point(400, 800);
	}

	@Override
	protected void okPressed() {
		super.okPressed();
	}
}
